import UIKit
import Flutter
import AVFoundation

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        // 1.获取FlutterViewController(是应用程序的默认Controller)
        let controller = window.rootViewController as! FlutterViewController
        
        // 2.获取MethodChannel(方法通道)
        let batteryChannel = FlutterMethodChannel(
            name: "channel:Chenli",
            binaryMessenger: controller.binaryMessenger as FlutterBinaryMessenger)
        
        // 3.监听方法调用(会调用传入的回调函数)
        batteryChannel.setMethodCallHandler { [self] (call, result) in
            
            if call.method == "openWechat" {
                // 打开微信
                openWechat()
                result("1111111wechat")
                
            } else {
                result(NSNumber(value: 10))
            }
        }
        
        GeneratedPluginRegistrant.register(with: self)
        
        // 常驻后台
        loadMusicPlay()
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    func openWechat() {
        guard let url = URL(string: "weixin://") else {
            return
        }
        
        //先判断是否能打开该url
        if UIApplication.shared.canOpenURL(url) {
            //打开微信
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
                
            } else {
                UIApplication.shared.openURL(url)
            }
            
            let alert = UIAlertController(title: "提示", message: "打开微信", preferredStyle: UIAlertController.Style.alert);
            let alertAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            
            alert.addAction(alertAction)
            window.rootViewController?.present(alert, animated: true, completion: nil)
            
        } else {
            let alert = UIAlertController(title: "提示", message: "您的设备未安装微信APP", preferredStyle: UIAlertController.Style.alert);
            let alertAction = UIAlertAction(title: "确定", style: .cancel, handler: nil)
            
            alert.addAction(alertAction)
            window.rootViewController?.present(alert, animated: true, completion: nil)
        }
    }
    
    var audioPlayer = AudioManager.sharedManager.audioPlayer
    
    func loadMusicPlay() {
        let path = Bundle.main.path(forResource: "music", ofType: "mp3")
        let pathURL=NSURL(fileURLWithPath: path!)
        
        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playAndRecord)
            try AVAudioSession.sharedInstance().setActive(true)
            
            audioPlayer = try AVAudioPlayer(contentsOf: pathURL as URL)
            audioPlayer?.numberOfLoops = -1;
            audioPlayer?.prepareToPlay()
            audioPlayer?.play()
            
        } catch {}
    }
}

class AudioManager: NSObject, AVAudioPlayerDelegate {
    static let sharedManager = AudioManager()
    var audioPlayer: AVAudioPlayer!
}
